package com.ezjie.ielts.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.core.helper.AppDataBaseHelper;
import com.ezjie.ielts.model.FullTextInfo;

public class FullTextInfoDB {
	private static AppDataBaseHelper dbHelper;
	private SQLiteDatabase db;

	public FullTextInfoDB(Context context) {
		dbHelper = AppDataBaseHelper.getInstance(context);
	}

	// 查找全文
	public synchronized ArrayList<FullTextInfo> query(List<String> idLists) {
		db = dbHelper.getReadableDatabase();

		ArrayList<FullTextInfo> lists = new ArrayList<FullTextInfo>();

		for (int i = 0; i < idLists.size(); i++) {
			String sql = "select * from " + AppDataBaseHelper.FULLTEXT + " where "
					+ (idLists == null ? "" : "category_id=" + idLists.get(i));
			Cursor cursor = db.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				FullTextInfo info = new FullTextInfo();
				info.category_id = cursor.getString(1);
				info.content = cursor.getString(2);
				info.title = cursor.getString(3);
				info.title_en = cursor.getString(4);
				info.android_img = cursor.getString(5);
				info.ios_img = cursor.getString(6);
				info.con_img = cursor.getString(7);
				lists.add(info);
			}
			cursor.close();
		}
		db.close();

		return lists;
	}

	// 删除全文
	public synchronized void delete() {
		db = dbHelper.getWritableDatabase();

		String sql = "delete from " + AppDataBaseHelper.FULLTEXT;
		db.execSQL(sql);
		db.close();
	}

	// 更新全文
	public synchronized void update() {
		db = dbHelper.getWritableDatabase();

		String sql = "update " + AppDataBaseHelper.FULLTEXT + " set content= ? ";
		db.execSQL(sql, new Object[] { 1 });
		db.close();
	}

	/**
	 * 插入全文
	 * 
	 * @param category
	 */
	public synchronized void insert(FullTextInfo fullTextInfo) {
		db = dbHelper.getWritableDatabase();

		// db.beginTransaction();
		String sql = "insert into " + AppDataBaseHelper.FULLTEXT
				+ " (category_id,content,title,title_en,android_img,ios_img,con_img) " + "values(?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { fullTextInfo.category_id, fullTextInfo.content, fullTextInfo.title,
				fullTextInfo.title_en, fullTextInfo.android_img, fullTextInfo.ios_img, fullTextInfo.con_img });
		db.close();
	}

	/**
	 * 更新问题组
	 * 
	 */
	public synchronized void replaceInto(List<FullTextInfo> fullTextInfos) {
		db = dbHelper.getWritableDatabase();

		for (int i = 0; i < fullTextInfos.size(); i++) {
			String sql = "replace into " + AppDataBaseHelper.FULLTEXT
					+ " (category_id,content,title,title_en,android_img,ios_img,con_img) " + "values(?,?,?,?,?,?,?)";
			db.execSQL(sql, new Object[] { fullTextInfos.get(i).category_id, fullTextInfos.get(i).content,
					fullTextInfos.get(i).title, fullTextInfos.get(i).title_en, fullTextInfos.get(i).android_img,
					fullTextInfos.get(i).ios_img, fullTextInfos.get(i).con_img });
		}

		db.close();
	}
}
